<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/"> </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-button type="primary" icon="el-icon-delete" class="handle-del mr10" @click="delAllSelection">批量删除</el-button>
                <el-button type="primary" icon="el-icon-download" @click="downExecl">导出</el-button>
                <el-input v-model="query.search" placeholder="商品名称" class="handle-input mr10"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
                <el-button type="primary" icon="el-icon-lx-add" @click="add" style="float: right">上架商品</el-button>
            </div>
            <el-table
                :data="tableData.records"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column prop="goodId" label="商品Id"></el-table-column>

                <!-- <el-table-column prop="pwd" label="密码"></el-table-column> -->
                <el-table-column prop="name" label="商品名称"></el-table-column>
                <el-table-column prop="subtitle" label="副标题"></el-table-column>
                <el-table-column prop="price" label="商品价格"></el-table-column>
                <el-table-column label="商品分类">
                    <template slot-scope="scope">
                        {{ categories[scope.row.categoryId] }}
                    </template>
                </el-table-column>
                <el-table-column prop="productStock" label="库存"></el-table-column>
                <!-- <el-table-column prop="imageHead" label="商品图片"></el-table-column> -->
                <el-table-column label="商品图片" align="center">
                    <template slot-scope="scope">
                        <el-image class="table-td-thumb" :src="scope.row.imageFoot" :preview-src-list="[scope.row.imageFoot]"></el-image>
                    </template>
                </el-table-column>
                <el-table-column label="状态" align="center">
                    <template slot-scope="scope">
                        <el-tag :type="scope.row.status == 1 ? 'success' : 'danger'">{{ scope.row.status == 1 ? '可用' : '禁用' }}</el-tag>
                    </template>
                </el-table-column>

                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <div v-if="scope.row.status == 1">
                            <el-button type="text" icon="el-icon-edit" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                            <el-button type="text" icon="el-icon-delete" class="red" @click="handleDelete(scope.$index, scope.row)"
                                >下架</el-button
                            >
                        </div>
                        <el-button v-else class="yellow" type="text" icon="el-icon-edit" @click="rec(scope.row.goodId)">还原</el-button>
                    </template>
                </el-table-column>
            </el-table>

            <div class="pagination">
                <el-pagination
                    style="margin-top: 10px"
                    background
                    :current-page.sync="query.pageNum"
                    :page-count="tableData.pages"
                    layout="prev,pager,next"
                    @current-change="getData"
                ></el-pagination>
            </div>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog title="编辑" :visible.sync="editVisible" width="30%">
            <el-form ref="form" :model="form" label-width="70px">
                <el-form-item label="商品名称">
                    <el-input v-model="form.name"></el-input>
                </el-form-item>
                 <el-form-item label="副标题">
                    <el-input v-model="form.subtitle"></el-input>
                </el-form-item>
                <el-form-item label="价格">
                    <el-input v-model="form.price"></el-input>
                </el-form-item>
                <el-form-item label="库存">
                    <el-input v-model="form.productStock"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveEdit">确 定</el-button>
            </span>
        </el-dialog>
        <!-- 编辑商品增加 -->
        <el-dialog title="增加商品" :visible.sync="addVisible" width="30%">
            <el-form ref="form" :model="form" label-width="70px">
                <el-form-item label="商品Id">
                    <el-input v-model="form.goodId"></el-input>
                </el-form-item>
                <el-form-item label="商品名称">
                    <el-input v-model="form.name"></el-input>
                </el-form-item>
                <el-form-item label="价格">
                    <el-input v-model="form.price"></el-input>
                </el-form-item>
                <el-form-item label="商品分类">
                    <el-select v-model="form.categoryId" placeholder="请选择">
                        <el-option v-for="(name, value) in categories"  :key="value" :value="value" :label="name"> </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="副标题">
                    <el-input v-model="form.subtitle"></el-input>
                </el-form-item>
                <el-form-item label="库存">
                    <el-input v-model="form.productStock"></el-input>
                </el-form-item>
                <el-form-item label="商品图片">
                    <el-upload v-model="form.file" action="#" list-type="picture-card" :auto-upload="false"
					:multiple="false" :on-change="changeIcon" :on-remove="removeIcon" :limit="1" :file-list="fileList">
					<i slot="default" class="el-icon-plus"></i>
				</el-upload>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="addVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveAdd">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import { fetchData } from '../../api/index';
import { getUsers } from '../../api';
import request from '@/utils/request';
import qee from 'qf-export-excel';
export default {
    name: 'goods',
    data() {
        return {
            query: {
                search: '',
                pageNum: 1,
                pageSize: 12
            },
            tableData: [],
            categories: [],
            multipleSelection: [],
            delList: [],
            editVisible: false,
            addVisible: false,
            formLabelWidth: '120px',
            form: {},
            fileList:[],
            addForm:{},
            idx: -1,
            id: -1
        };
    },
    created() {
        this.getData();
        this.getCategories();
    },
    methods: {
        // 获取 easy-mock 的模拟数据
        getData() {
            request
                .get('http://localhost:8081/goods/getGoods', {
                    params: this.query
                })
                .then((res) => {
                    this.$message.success('成功');
                    this.tableData = res.data;
                });
        },
        getCategories() {
            request
                .get('http://localhost:8081/category/getData', {
                    params: this.query
                })
                .then((res) => {
                    this.categories = res.data;
                });
        },
        // 触发搜索按钮
        handleSearch() {
            this.getData();
        },
        // 删除操作
        handleDelete(index, row) {
            // 二次确认删除
            this.$confirm('确定要下架该商品吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    let form = new FormData();
                    form.append('goodId', row.goodId);
                    request.post('http://localhost:8081/goods/del', form).then((res) => {
                        this.$message.success('删除成功');
                        this.getData();
                    });
                })
                .catch(() => {});
        },
        rec(id) {
            // 还原
            this.$confirm('确定要还原吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    let form = new FormData();
                    form.append('goodId', id);
                    request.post('http://localhost:8081/goods/rec', form).then((res) => {
                        this.$message.success('还原成功');
                        this.getData();
                    });
                })
                .catch(() => {});
        },
        // 多选操作
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        delAllSelection() {
            const length = this.multipleSelection.length;
            let str = '';
            this.delList = this.delList.concat(this.multipleSelection);
            const goodids = [];
            this.delList.forEach((item) => {
                goodids.push(item.goodId);
            });
            request
                .post('http://localhost:8081/goods/mulDel', this.qs.stringify({ ids: goodids }, { arrayFormat: 'repeat' }))
                .then((res) => {
                    this.$message.success('删除成功');
                    this.getData();
                });
        },
        changeIcon(file) {
				this.form.file = file.raw
			},
		removeIcon() {
				this.form.file = null
			},
        add() {
            this.addVisible = true;
        },
        saveAdd() {
            let param = new FormData();
			for (const key in this.form) {
				param.append(key, this.form[key])
			}
            request
                .post('http://localhost:8081/goods/add', param)
                .then((res) => {
                    console.log(res)
                    this.addVisible = false;
                    this.getData();
                });

        },
        // 编辑操作
        handleEdit(index, row) {
            this.idx = index;
            this.form = row;
            this.editVisible = true;
        },
        // 保存编辑
        saveEdit() {
            let params = new FormData();
            for (const key in this.form) {
                params.append(key, this.form[key]);
            }
            request.post('http://localhost:8081/goods/update', params).then((res) => {
                this.$message.success('修改成功');
                this.editVisible = false;
                this.getData();
            });
        },
        // 分页导航
        handlePageChange(val) {
            this.$set(this.query, 'pageIndex', val);
            this.getData();
        },
        downExecl() {
            const titleList = [
                { title: '商品Id', key: 'goodId' },
                { title: '商品名称', key: 'name' },
                { title: '商品价格', key: 'price' },
                { title: '商品分类', key: 'categoryId' },
                { title: '库存', key: 'productStock' }
            ];
            qee(titleList, this.tableData.records, 'goods');
        }
    }
};
</script>

<style scoped>
.el-dropdown-link {
    cursor: pointer;
    color: #409eff;
}
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 200px;
    display: inline-block;
    margin-left: 20px;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.yellow {
    color: rgb(136, 138, 18);
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
